<?php
class UtilController extends Zend_Controller_Action{

	public static function getErrorMessage($id){
		$alerta = new AlertaErro();
		$alerta->load($id);
		return $alerta->ds_mensagem;
	}
	
	function statusAction(){
		$this->view->operacao = $this->_getParam('operacao');
		$this->view->mensagem = $this->_getParam('mensagem');
		$this->view->links = $this->_getParam('links');
		$this->view->linkVoltar = null;
		$this->view->title 	= null;
        $this->view->sair = null;
        $this->render();
	}	
	
	
	function expositorAction(){
		$this->_helper->viewRenderer->setNoRender();
		$idExpositor = $this->_getParam('parametro1');
		$idEvento    = $this->_getParam('parametro2');
		$html = $this->getOptionExpositorPorEvento($idExpositor, $idEvento);
		$this->getResponse()->setHeader('Content-Type', 'text/plain;charset=utf-8');
        $this->getResponse()->setBody($html);
        return;
	}

	function paisesAction(){
		$this->_helper->viewRenderer->setNoRender();
		if (isset($_REQUEST['query']) && $_REQUEST['query'] != null && strlen($_REQUEST['query'])>0){
			$pais = new Pais();
			$q  = $_REQUEST['query'];
			$html = '<ul>'. "\n";
			foreach ($pais->buscar($q)as $item){
				$p=$item['ds_pais'];
				$p = preg_replace('/(' . $q . ')/i', '<span style="font-weight:bold;">$1</span>', $p);
				$html.="\t".'<li id="autocomplete_'.$item['id_pais'].'" rel="'.$item['id_pais'].'">'.$p.'</li>'."\n";
			}
			$html.='</ul>';
        	$this->getResponse()->setBody($html);
		}
        return;
	}

	function estadosAction(){
		$this->_helper->viewRenderer->setNoRender();
		$idPais = $this->_getParam('extraParam');
		if ($idPais!=null && isset($_REQUEST['query']) && $_REQUEST['query'] != null && strlen($_REQUEST['query'])>0){
			$estado = new Estado();
			$q  = $this->_getParam('query');
			$html = '<ul>'. "\n";
			foreach ($estado->buscar($q, $idPais)as $item){
				$p=$item['ds_estado'];
				$p = preg_replace('/(' . $q . ')/i', '<span style="font-weight:bold;">$1</span>', $p);
				$html.="\t".'<li id="autocomplete_'.$item['id_estado'].'" rel="'.$item['id_estado'].'">'. $p .'</li>'."\n";
			}
			$html.='</ul>';
			//$this->getResponse()->setHeader('Content-Type', 'text/plain;charset=utf-8');
        	$this->getResponse()->setBody($html);
		}
        return;
	}

	function cidadesAction(){
		$this->_helper->viewRenderer->setNoRender();
		$idEstado = $this->_getParam('extraParam');
		if ($idEstado!=null && isset($_REQUEST['query']) && $_REQUEST['query'] != null && strlen($_REQUEST['query'])>0){
			$cidade = new Cidade();
			$q  = $this->_getParam('query');
			$html = '<ul>'. "\n";
			foreach ($cidade->buscar($q, $idEstado)as $item){
				$p=$item['ds_cidade'];
				$p = preg_replace('/(' . $q . ')/i', '<span style="font-weight:bold;">$1</span>', $p);
				$html.="\t".'<li id="autocomplete_'.$item['id_cidade'].'" rel="'.$item['id_cidade'].'">'. $p .'</li>'."\n";
			}
			$html.='</ul>';
			$this->getResponse()->setHeader('Content-Type', 'text/plain;charset=utf-8');
        	$this->getResponse()->setBody($html);
		}
        return;
	}

	function fumturAction(){
		$this->_helper->viewRenderer->setNoRender();
		$idEvento   = $this->_getParam('evento');
		$idOperador = $this->_getParam('operador');
		$html = $this->getOptionFumturPorEvento($idOperador, $idEvento, 0, 1);
		$this->getResponse()->setHeader('Content-Type', 'text/plain;charset=utf-8');
        $this->getResponse()->setBody($html);
        return;
	}


/*
	public static function getEstadoDaCidade($id_cidade){
		$cidade = new Cidade();
		foreach ($cidade->buscar($id_cidade, null) as $row){
			$id_estado=$row['id_estado'];
		}
		return $id_estado;
	}

	public static function getPaisDoEstado($id_estado){
		$id_pais="";
		$estado = new Estado();
		foreach ($estado->buscar(null, $id_estado) as $row){
			$id_pais=$row['id_pais'];
		}
		return $id_pais;
	}
*/
	public static function getCategoria(){
		$categoria = new CategoriaParticipante();
		return $categoria->buscar();
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/*Inicio formatação data*/
	public static function brDateUsDate ($date) {
	  if ($date)
	    return substr($date,6,4).'-'.substr($date,3,2).'-'.substr($date,0,2);
	  else
	    return '';
	}

	public static function usDateBrDate ($date) {
	  if ($date)
	    return substr($date,8,2).'/'.substr($date,5,2).'/'.substr($date,0,4);
	  else
	    return '';
	}
	/*Fim formatação data*/
	/*Inicio Funções Options*/
	public static function getOptionCategoria($idCategoria, $pesquisa=0){
		$categoria = new CategoriaParticipante();
		if ($pesquisa==0){
			$html=' <option value="0">--Selecione>>>></option> ';
		} else {
			$html=' <option value="0">--Todos--</option> ';
		}
		foreach ($categoria->buscar() as $item){
			$html.=' <option ';
			if ($idCategoria==$item['id_categoria']){
				$html.='selected="selected"';
			}
			$html.=' value="'.$item['id_categoria'].'">'.$item['ds_categoria'].'</option> ';
		}
		return $html;
	}

	public static function getOptionEvento($id_evento, $validos, $pesquisa){
		$evento = new Evento();
		if ($pesquisa==0){
			$html=' <option value="0">--Selecione evento--</option> ';
		} elseif ($pesquisa==1) {
			$html=' <option value="0">--Todos--</option> ';
		}
		$rows=$evento->buscar($id_evento, null, null, null, null, 1, 1, $validos);
		foreach ($rows as $item){
			$html.='<option ';
			if ($id_evento==$item['id']){
				$html.='selected="selected"';
			}
			$html.=' value="'.$item['id'].'">'.$item['nm_evento'].'</option>';
		}
		return $html;

	}

	public static function getOptionAtuacao($id_atuacao, $pesquisa=0){
		$atuacao = new RamoAtuacao();
		if ($pesquisa==0){
			$html=' <option value="0">--Selecione ramo de atuação--</option> ';
		} else {
			$html=' <option value="0">--Todos--</option> ';
		}
		foreach ($atuacao->buscar() as $item){
			$html.= '<option ';
			if($id_atuacao==$item['id_atuacao']){
				$html.='selected="selected"';
			}
				$html.=' value="'.$item['id_atuacao'].'">'.$item['ds_atuacao'].'</option>';
		}
		return $html;
	}

	public static function getOptionCivil($id_civil, $pesquisa=0){
		$civil = new Civil();
		if ($pesquisa==0){
			$html=' <option value="0"> </option> ';
		} else {
			$html=' <option value="0">--Todos--</option> ';
		}
		foreach ($civil->buscar() as $item){
			$html.=' <option ';
			if ($id_civil==$item['id_civil']){
				$html.='selected="selected"';
			}
			$html.=' value="'.$item['id_civil'].'">'.$item['ds_civil'].'</option> ';
		}
		return $html;
	}

	public static function getOptionSexo($id_sexo, $pesquisa=0){
		$sexo = new Sexo();
		if ($pesquisa==0){
			$html=' <option value="0"> </option> ';
		} else {
			$html=' <option value="0">--Ambos--</option> ';
		}
		foreach ($sexo->buscar() as $item){
			$html.=' <option ';
			if ($id_sexo==$item['id_sexo']){
				$html.='selected="selected"';
			}
			$html.=' value="'.$item['id_sexo'].'">'.$item['ds_sexo'].'</option> ';
		}
		return $html;
	}
	
	public static function getOptionLocal($id_local, $pesquisa=0){
		$local = new LocalEvento();
		if ($pesquisa==0){
			$html=' <option value="0">--Selecione local do evento--</option> ';
		} else {
			$html=' <option value="0">--Todos--</option> ';
		}
		foreach ($local->buscar(null, null, null, 1, 0) as $item){
			$html.=' <option ';
			if ($id_local==$item['id']){
				$html.='selected="selected"';
			}
			$html.=' value="'.$item['id'].'">'.$item['local'].'</option> ';
		}
		return $html;
	}
	
	public static function getOptionStatus($id_status,  $pesquisa=0){
		$status = new Status();
		if ($pesquisa==0){
			$html=' <option value="0">--Selecione>>>></option> ';
		} else {
			$html=' <option value="0">--Todos--</option> ';
		}
		foreach ($status->buscar() as $item){
			$html.=' <option ';
			if ($id_status==$item['id_status']){
				$html.='selected="selected"';
			}
			$html.=' value="'.$item['id_status'].'">'.$item['nm_status'].'</option> ';
		}
		return $html;
	}
		
	public static function getOptionJuridica($idJuridica, $pesquisa=0){
		$juridica = new Juridica();
		if ($pesquisa==0){
			$html=' <option value="0">--Selecione>>></option> ';
		} else {
			$html=' <option value="0">--Todos--</option> ';
		}
		foreach ($juridica->buscar(null, null, null, null, null, null, null, null, 1, 1) as $item){
			$html.=' <option ';
			if ($idJuridica==$item['id_juridica']){
				$html.='selected="selected"';
			}
			$html.=' value="'.$item['id_juridica'].'">'.$item['ds_fantasia'].'</option> ';
		}
		return $html;
	}
	/*Fim Funções Options*/


	public static function alert($msgText){
		echo '<script>alert("'.$msgText.'")</script>';
	}

	public static function trataLinkOrdenacao($link, $ordemAtual, $novaOrdem, $ascDesc, $tabIndex=null){
		if ($ordemAtual == $novaOrdem){
			if ($ascDesc == 0)
				$ascDesc = 1;
			else
				$ascDesc = 0;
		}
		else{
			$ascDesc = 0;
		}

		if (strpos($link, '?') === false){
			$link .= '?';
		}
		else {
			$link .= '&';
		}
		$link .= "order=".$novaOrdem ."&asc=". $ascDesc. (is_null($tabIndex) ? null : "&tabIndex=".$tabIndex) ;
		return $link;
	}

	public static function getTableStatus(){
		$status = new Status();
		$html ='<table>';
		foreach ($status->buscar() as $item){
			$html.='<tr><td>'.UtilController::getCorStatus($item['id_status'], $item['ds_status']).'  '.$item['nm_status'].'</td></tr>';

		}
		$html.=' </table>';
		return $html;
	}

	public static function getCorStatus($status, $dsStatus){
		if ($status==1){
			$nm_image='led_verde';
		} elseif ($status==3){
			$nm_image='led_amarelo';
		} elseif ($status==2){
			$nm_image='led_vermelho';
		}
		$html='<img src="/images/'.$nm_image.'.png" border="0" title="'.$dsStatus.'"></a>';
		return $html;
	}



	public static function getOptionFumturPorEvento($idOperador, $idEvento, $idFumtur, $pesquisa=0){
		$fumtur = new Fumtur();
		$result = $fumtur->buscar($idOperador, null, $idEvento, null, null, null, 1, 1);
		if (count($result)==0){
			$html='<option value="0">Não possui FUMTURs para este evento</option>';
		} else {
			if (($idEvento==0) && ($idFumtur==0)){
				$html='<option value="0">--Selecione evento--</option> ';
			} else {
				if ($pesquisa==0){
					$html=' <option value="0">--Selecione FUMTUR--</option> ';
				} else {
					$html=' <option value="0">--Todos--</option> ';
				}
				foreach ($result as $item){
					$html.=' <option ';
					if ($idFumtur==$item['id_fumtur']){
						$html.='selected="selected"';
					}
					$html.=' value="'.$item['id_fumtur'].'">'.$item['ds_nome'].' ('.$item['ds_cidade'].'-'.$item['ds_sigla'].')</option> ';
				}
			}
		}
		return $html;
	}
/*
	public static function campoObrigatorio(){
		return '<b style="color: red; font-size: large;">*</b>';
	}
*/
	public static function getOptionExpositorPorEvento($idExpositor, $idEvento, $pesquisa=0){
		$expositor = new Expositor();
		$result = $expositor->_buscar(null, $idEvento);
		if (count($result)==0){
			$html='<option value="0">Não possui Expositores para este evento</option>';
		} else {
			if (($idEvento==0) && ($idExpositor==0)){
				$html='<option value="0">--Primeiro selecione o evento--</option> ';
			} else {
				if ($pesquisa==0){
					$html=' <option value="0">--Selecione EXPOSITOR--</option> ';
				} else {
					$html=' <option value="0">--Todos--</option> ';
				}
				foreach ($result as $item){
					$html.=' <option ';
					if ($idExpositor==$item['id_juridica']){
						$html.='selected="selected"';
					}
					$html.=' value="'.$item['id_juridica'].'">'.$item['ds_fantasia'].' ('.$item['ds_cidade'].'-'.$item['ds_sigla'].')</option> ';
				}
			}
		}
		return $html;
	}
}
